package com.jason.practice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jason.practice.model.GoodsStockInfo;
import com.jason.practice.mapper.GoodsStockInfoMapper;
import com.jason.practice.response.Response;
import com.jason.practice.service.GoodsStockInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jason.practice.utils.SpringRestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiayb_9169@126.com
 * @since 2022-09-29
 */
@Service
public class GoodsStockInfoServiceImpl extends ServiceImpl<GoodsStockInfoMapper, GoodsStockInfo> implements GoodsStockInfoService {
    @Override
    public Response handleGoodsStock(String skusId) {
//        String skusId = "100026292344";
        String serviceUrl = "https://wq.jd.com/commodity/skudescribe/get?callback=reaStockAnPriceCbA&command=3&source=wqm_search&priceinfo=1&buynums=dwsds&skus={skusId}";
        serviceUrl = serviceUrl.replace("{skusId}", skusId);
        String result = SpringRestUtils.getForObject(serviceUrl,String.class);
        if (StringUtils.isNoneBlank(result)) {
            result = result.replaceAll("reaStockAnPriceCbA","");
            result = result.substring(1,result.lastIndexOf(")"));
        }
        JSONObject resultObj = JSONObject.parseObject(result);
        if (ObjectUtils.isNotEmpty(resultObj)) {
            GoodsStockInfo goodsStockInfo = new GoodsStockInfo();
            JSONObject stockStateObj = resultObj.getJSONObject("stockstate");
            if (ObjectUtils.isNotEmpty(stockStateObj)) {
                JSONObject dataObj = stockStateObj.getJSONObject("data");
                if(ObjectUtils.isNotEmpty(dataObj)) {
                    JSONObject skusIdObj =  dataObj.getJSONObject(skusId);
                    goodsStockInfo.setStockStateInfo(skusIdObj.getString("a"));
                }
            }
            JSONObject priceinfoObj = resultObj.getJSONObject("priceinfo");
            if (ObjectUtils.isNotEmpty(priceinfoObj)) {
                JSONObject priceinfoDataInfo = priceinfoObj.getJSONObject("data");
                if(ObjectUtils.isNotEmpty(priceinfoDataInfo)) {
                    JSONObject skusIdObj =  priceinfoDataInfo.getJSONObject(skusId);
                    goodsStockInfo.setPriceInfoP(skusIdObj.getString("p"));
                    goodsStockInfo.setPriceInfoOp(skusIdObj.getString("op"));
                    goodsStockInfo.setPriceInfoM(skusIdObj.getString("m"));
                    goodsStockInfo.setPriceInfoUp(skusIdObj.getString("up"));
                }
            }
            goodsStockInfo.setSkusId(skusId);
            goodsStockInfo.setTraceId(resultObj.getString("traceId"));
            baseMapper.insert(goodsStockInfo);
            return Response.ok(null, "保存成功！");
        } else {
            return Response.failed("接口解析失败！");
        }

    }
}
